package com.yifushidai.service.impl;

import com.github.pagehelper.PageHelper;
import com.yifushidai.annotation.UserHolder;
import com.yifushidai.entity.ApiUniqueMac;
import com.yifushidai.mapper.ApiUniqueMacMapper;
import com.yifushidai.service.ApiUniqueMacService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ApiUniqueMacServiceImpl extends BaseService<ApiUniqueMac> implements ApiUniqueMacService {

    @Autowired
    private ApiUniqueMacMapper apiUniqueMacMapper;

    @Override
    public int saveOrUpdate(ApiUniqueMac record) {
        // 创建Example
        Example example = new Example(ApiUniqueMac.class);
        // 创建Criteria 添加条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mac", record.getMac());

        List<ApiUniqueMac> db = apiUniqueMacMapper.selectByExample(example);

        if (db.isEmpty()) {//新建
            return saveNotNull(record);
        } else {//编辑
            return updateByExampleSelective(record, example);
        }
    }

    @Override
    public int deleteByMac(ApiUniqueMac record) {
        // 创建Example
        Example example = new Example(ApiUniqueMac.class);

        // 创建Criteria 添加条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mac", record.getMac());

        return deleteByExample(example);
    }

    @Override
    public List<ApiUniqueMac> searchByAlias(String alias) {
        // 创建Example
        Example example = new Example(ApiUniqueMac.class);
        example.setOrderByClause("bandtime DESC");

        // 创建Criteria 添加条件
        Example.Criteria criteria = example.and();
        criteria.andEqualTo("mobile", UserHolder.get().getMobile());
        if (StringUtils.isNotBlank(alias)) {
            criteria.andLike("alias", "%" + alias + "%");
        }

        return selectByExample(example);
    }

}